﻿using Beryl.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls.WebParts;

namespace Beryl.Model
{
    //裁切开工记录
    public class CuttingStart
    {
        private static CuttingStart instance = null;
        private static readonly object uniqueLock = new object();
        public static CuttingStart Instance
        {
            get
            {
                lock (uniqueLock)
                {
                    if (instance == null)
                    {
                        instance = new CuttingStart();
                    }
                }
                return instance;
            }
        }
        public CuttingStart() { }

        //查找裁切单
        public string selectbarCode(string barCode)
        {

            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            string strPicYZ;
            try
            {
                string sql = @"select a.cMOLotNo,a.cBatchNumber,a.cOFlag,b.leng,a.dEffWidth,a.dLotQty," +
                    "a.cxx,a.csx,a.cMatNo from APS_ProLotOrder a join(select max(dLength) as leng,cOrderNo " +
                    "from[dbo].[APS_ValidBOM] group by cOrderNo) b on a.cMOLotNo = b.cOrderNo where a.cBarCode = '{0}'";

                string sql1 = string.Format(sql, barCode);
                strPicYZ = JsonConvert.SerializeObject(CommonMethod.Instance.selectDatabase(sql1));//转为json
                LogUtil.Log(name + strPicYZ);//将方法名写进
                if (strPicYZ.Equals("[]"))
                {
                    return "裁切单号为空";
                }
                else
                {
                    Newtonsoft.Json.Linq.JArray jsonArr = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(strPicYZ);
                    string cOFlag = jsonArr[0]["cOFlag"].ToString();
                    if (!cOFlag.Equals("0"))
                    {
                        return "工单已开始不能重复开工";
                    }
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
            //int t =  strPicYZ.LastIndexOf("[");//索引为1
            strPicYZ = strPicYZ.Remove(0, 1);
            strPicYZ = strPicYZ.Remove(strPicYZ.Length - 1, 1);
            return strPicYZ;
        }

        //查找刀具
        public string selectgetKn(string kn)
        {
       
            Newtonsoft.Json.Linq.JArray jsonArr = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(kn);
            var k = jsonArr[0];
            string sql = string.Format(@"select * from PMS_BladeGroup where cBladeGroupNum = '{0}'", k["刀具"].ToString().Split('-')[1]);
            var Kn = CommonMethod.Instance.selectDatabase(sql);
            var KnRow = Kn.Select($"cBladeGroupNum = '{k["刀具"].ToString().Split('-')[1]}'");
            string Long = k["长度"].ToString();
            string isOK;
            if (KnRow.Length > 0)
            {
                var Kn1 = KnRow[0];
                if (Kn1["cStatue"].ToString().Equals("报废") || Kn1["cStatue"].ToString().Equals("废弃"))
                {
                    isOK = "刀具已报废或已废弃";
                }
                else
                {
                    string dStandardMeter = Kn1["dStandardMeter"].ToString();
                    string dCumulativeMeter = Kn1["dCumulativeMeter"].ToString();
                    Dictionary<string, string> dic = new Dictionary<string, string>();
                    isOK = CommonMethod.Instance.checkKn(dStandardMeter, Long, dCumulativeMeter);
                    dic.Add("状态", isOK);
                }
            }
            else
            {
                isOK = "刀具不存在!";
            }

            return isOK;
        }

        //裁切开工记录提交逻辑
        public string summit(string summit)
        {
            string name = System.Reflection.MethodBase.GetCurrentMethod().Name;
            try
            {
                Newtonsoft.Json.Linq.JArray jsonArr = (Newtonsoft.Json.Linq.JArray)JsonConvert.DeserializeObject(summit);
                var k = jsonArr[0];
                //string sql = "insert into PMS_CutStartRecord (cBarCode,cMOLotNo,cStartTotalLength,cToolNo,cEqNo,cPsNo,cPsName,cCreateTime,isComplete) values" +
                //    " ('" + k["cBarCode"] + "'," + k["cMOLotNo"] + ")";
                string result = CommonMethod.Instance.KnStatus(k["cToolNo"].ToString(), k["cStartTotalLength"].ToString());
                string sql = string.Format(@"insert into PMS_CutStartRecord (cBarCode,cMOLotNo,cStartTotalLength,cToolNo,cEqNo,cPsNo,cPsName,cCreateTime,isComplete) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')",
                    k["cBarCode"].ToString(), k["cMOLotNo"].ToString(), k["cStartTotalLength"].ToString(),
                    k["cToolNo"].ToString(), k["cEqNo"].ToString(), k["cPsNo"].ToString(), k["cPsName"].ToString(), k["cCreateTime"].ToString(), k["isComplete"].ToString());

                string sql1 = string.Format(@"update APS_CutProLotOrder set isReport = '1' where cOrderNo = '{0}'", k["cMOLotNo"].ToString());

                string sql2 = string.Format(@"update APS_ProLotOrder set cOFlag = 1 where cOrderNo = '{0}'", k["cMOLotNo"].ToString());
                string sql3 = "";
                if (!result.Contains("报废"))
                {
                    sql3 = string.Format(@"update PMS_BladeGroup set cStatue = '{0}',dCumulativeMeter = '{1}' where cBladeGroupNum = '{2}'","正常", result, k["cToolNo"].ToString());
                }else
                {
                    sql3 = string.Format(@"update PMS_BladeGroup set cStatue = '{0}' where cBladeGroupNum = '{1}'", result, k["cToolNo"].ToString());
                }
                string sql4 = sql + ";" + sql1 + ";" + sql2 + ";" + sql3;

                SqlHelper.IniConnString();
                var connstr = SqlHelper.GetConnSting();
                SqlHelper.ExecuteNonQuery(connstr, CommandType.Text, sql4);
                LogUtil.Log(name + "插入成功");
                return "[0]";
            }
            catch (Exception e)
            {
                LogUtil.Error(e.Message);
                return e.Message;
            }
        }
    }
}